package com.minhphuong.dal;

import com.minhphuong.entities.HoaDon;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class HoaDonDAL {

    public static List<HoaDon> getAllHoaDon() {
        List<HoaDon> listHoaDon = null;
        Session session = null;

        try {
            session = HelperDAL.GetSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from HoaDon");
            listHoaDon = (List<HoaDon>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (listHoaDon.size() > 0) {
                for (HoaDon hoaDon : listHoaDon) {
                    Hibernate.initialize(hoaDon.getNguoiDungByMaNd());
                }
            }
            //Hibernate.initialize(sanPham.getHang());
            session.flush();
            session.close();
        }
        return listHoaDon;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public static HoaDon getHoaDonByID(String id) {
        HoaDon hoadon = null;
        Session session = null;

        try {
            session = HelperDAL.GetSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from HoaDon where CodeHoaDon=:id").setString("id", id);
            hoadon = (HoaDon) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (hoadon != null) {
                Hibernate.initialize(hoadon.getNguoiDungByMaNd().getHoTen());
            }
            session.flush();
            session.close();
        }
        return hoadon;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public static void updateHoaDon(HoaDon hoadon) {
        Session session = HelperDAL.GetSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.update(hoadon);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public static void insertHoaDon(HoaDon hoadon) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HelperDAL.GetSession();
            tx = session.beginTransaction();
            session.save(hoadon);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
}
